Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
@types/mysql
Advanced tools
TypeScript definitions for mysql
The @types/mysql package provides TypeScript type definitions for the mysql package, enabling better development experience with type checking and IntelliSense in TypeScript projects. It does not add any new functionality to mysql itself but allows developers to use mysql in TypeScript applications more effectively.
Connecting to a MySQL database
This code demonstrates how to connect to a MySQL database using the mysql package with TypeScript types provided by @types/mysql. It includes type checking for the connection options.
import * as mysql from 'mysql';
const connection = mysql.createConnection({
host: 'localhost',
user: 'yourusername',
password: 'yourpassword',
database: 'mydb'
});
connection.connect(err => {
if (err) throw err;
console.log('Connected!');
});
Performing a query
This example shows how to perform a basic SQL query to select all records from the 'users' table. The results are logged to the console. The @types/mysql package ensures that the function parameters and return types are correctly typed.
connection.query('SELECT * FROM users', (err, results) => {
if (err) throw err;
console.log(results);
});
Closing the connection
This snippet illustrates how to close the database connection properly. It uses the end method provided by the mysql package, with type safety ensured by @types/mysql.
connection.end(err => {
if (err) return console.log('error:' + err.message);
console.log('Close the database connection.');
});
mysql2 is a fast node.js driver for MySQL with full mysql protocol implementation. It provides promise-based and callback-based interfaces. Compared to @types/mysql, mysql2 comes with built-in TypeScript support, so it doesn't require separate type definitions.
typeorm is an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, and Electron platforms and can be used with TypeScript and JavaScript (ES5, ES6, ES7, ES8). It supports the MySQL database among others. Unlike @types/mysql, which only provides type definitions for the mysql package, TypeORM offers a higher-level abstraction for database interactions along with built-in type support.
sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite, and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication, and more. While @types/mysql provides type definitions for using mysql in TypeScript, Sequelize offers a comprehensive ORM solution with its own types for TypeScript integration.
npm install --save @types/mysql
This package contains type definitions for mysql (https://github.com/mysqljs/mysql).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/mysql.
These definitions were written by William Johnston, Krittanan Pingclasai, James Munro, and Sanders DeNardi.
FAQs
TypeScript definitions for mysql
We found that @types/mysql demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.